package com.lottery.im.module.tim.message;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import com.lottery.im.MyApplication;
import com.lottery.im.R;
import com.lottery.im.module.chat.adapter.ChatAdapter;
import com.tencent.imsdk.TIMGroupMemberInfo;
import com.tencent.imsdk.TIMGroupTipsElem;
import com.tencent.imsdk.TIMGroupTipsElemGroupInfo;
import com.tencent.imsdk.TIMGroupTipsElemMemberInfo;
import com.tencent.imsdk.TIMMessage;
import com.tencent.imsdk.TIMUserProfile;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 群tips消息
 */
public class GroupTipMessage extends Message {

  public final static String NO_TIP_TEXT = "#*@123#*@456";

  public GroupTipMessage(TIMMessage message) {
    this.message = message;
  }

  /**
   * 显示消息
   *
   * @param viewHolder 界面样式
   * @param context 显示消息的上下文
   */
  @Override
  public void showMessage(ChatAdapter.ViewHolder viewHolder, Context context) {
    viewHolder.leftPanel.setVisibility(View.GONE);
    viewHolder.rightPanel.setVisibility(View.GONE);

    if (getSummary().equals(NO_TIP_TEXT)) {
      viewHolder.systemMessage.setVisibility(View.GONE);
    } else {
      viewHolder.systemMessage.setVisibility(View.VISIBLE);
      viewHolder.systemMessage.setText(getSummary());
    }
  }

  /**
   * 获取消息摘要
   */
  @Override
  public String getSummary() {

    final TIMGroupTipsElem e = (TIMGroupTipsElem) message.getElement(0);
    StringBuilder stringBuilder = new StringBuilder();
    Iterator<Map.Entry<String, TIMGroupMemberInfo>>
        iterator = e.getChangedGroupMemberInfo().entrySet().iterator();
    switch (e.getTipsType()) {
      case CancelAdmin:

        if (e.getOpUser().equals(MyApplication.getInstance().getThirdAccId())) {
          stringBuilder.append(MyApplication.getInstance().getUserInfo().getUserName() + "取消");
        } else {
          stringBuilder.append(e.getOpGroupMemberInfo().getNameCard() + "取消");
        }
        StringBuilder member = new StringBuilder();
        Map<String, TIMUserProfile> changedUserInfo = e.getChangedUserInfo();
        for (TIMUserProfile profile : changedUserInfo.values()) {
          if (!TextUtils.isEmpty(member.toString())) member.append("、");
          member.append(profile.getNickName());
        }
        stringBuilder.append(member);
        stringBuilder.append("管理员身份");
        return stringBuilder.toString();
      case SetAdmin:
        if (e.getOpUser().equals(MyApplication.getInstance().getThirdAccId())) {
          stringBuilder.append(MyApplication.getInstance().getUserInfo().getUserName() + "设置");
        } else {
          stringBuilder.append(e.getOpGroupMemberInfo().getNameCard() + "设置");
        }
        StringBuilder user = new StringBuilder();
        Map<String, TIMUserProfile> changedUserInfo1 = e.getChangedUserInfo();
        for (TIMUserProfile profile : changedUserInfo1.values()) {
          if (!TextUtils.isEmpty(user.toString())) user.append("、");
          user.append(profile.getNickName());
        }
        stringBuilder.append(user);
        stringBuilder.append("为管理员");
        return stringBuilder.toString();

      //return MyApplication.getInstance().getString(R.string.summary_group_admin_change);
      case Join:
        //while (iterator.hasNext()) {
        //  Map.Entry<String, TIMGroupMemberInfo> item = iterator.next();
        //  stringBuilder.append(getName(e, item.getValue()));
        //  stringBuilder.append(" ");
        //}

        //for (String str : e.getUserList()) {
        //  if (!TextUtils.isEmpty(stringBuilder.toString())) stringBuilder.append("、");
        //  if (str.equals(MyApplication.getInstance().getThirdAccId())) {
        //    stringBuilder.append(MyApplication.getInstance().getUserInfo().getUserName());
        //  } else {
        //    if (FriendshipInfo.getInstance().isFriend(str)) {
        //      stringBuilder.append(FriendshipInfo.getInstance().getProfile(str).getNickName());
        //    } else {
        //      stringBuilder.append(str);
        //    }
        //  }
        //}

        Map<String, TIMUserProfile> addInfo = e.getChangedUserInfo();
        for (TIMUserProfile profile : addInfo.values()) {
          if (!TextUtils.isEmpty(stringBuilder.toString())) stringBuilder.append("、");
          stringBuilder.append(profile.getNickName());
        }

        return stringBuilder.toString() +
            MyApplication.getInstance().getString(R.string.summary_group_mem_add);
      case Kick:

        //for (String str : e.getUserList()) {
        //  if (!TextUtils.isEmpty(stringBuilder.toString())) stringBuilder.append("、");
        //  if (str.equals(MyApplication.getInstance().getThirdAccId())) {
        //    stringBuilder.append(MyApplication.getInstance().getUserInfo().getUserName());
        //  } else {
        //    if (FriendshipInfo.getInstance().isFriend(str)) {
        //      stringBuilder.append(FriendshipInfo.getInstance().getProfile(str).getNickName());
        //    } else {
        //      stringBuilder.append(str);
        //    }
        //  }
        //}

        Map<String, TIMUserProfile> kickInfo = e.getChangedUserInfo();
        for (TIMUserProfile profile : kickInfo.values()) {
          if (!TextUtils.isEmpty(stringBuilder.toString())) stringBuilder.append("、");
          stringBuilder.append(profile.getNickName());
        }

        // 踢出群聊 不显示
        return NO_TIP_TEXT;

      //return stringBuilder +
      //    MyApplication.getInstance().getString(R.string.summary_group_mem_kick);
      case ModifyMemberInfo:

        List<TIMGroupTipsElemMemberInfo> memberInfoList = e.getMemberInfoList();

        if (null != memberInfoList) {
          if (memberInfoList.get(0).getIdentifier()
              .equals(MyApplication.getInstance().getThirdAccId())) {
            // 自己被禁言
            if (memberInfoList.get(0).getShutupTime() > 0) {
              return e.getOpGroupMemberInfo().getNameCard() + "已对你禁言" + memberInfoList.get(0)
                  .getShutupTime() + "秒";
            }
            return e.getOpGroupMemberInfo().getNameCard() + "已对您解除禁言";
          } else {

            StringBuilder sb = new StringBuilder();
            Map<String, TIMUserProfile> silenceInfo = e.getChangedUserInfo();
            for (TIMUserProfile profile : silenceInfo.values()) {
              if (!TextUtils.isEmpty(sb.toString())) sb.append("、");
              sb.append(profile.getNickName());
            }

            if (memberInfoList.get(0).getShutupTime() > 0) {
              return e.getOpGroupMemberInfo().getNameCard() + "已对" + sb.toString() + "禁言"
                  + memberInfoList.get(0)
                  .getShutupTime() + "秒";
            } else {
              return e.getOpGroupMemberInfo().getNameCard() + "已对" + sb.toString() + "解除禁言";
            }
          }
        } else {
          while (iterator.hasNext()) {
            Map.Entry<String, TIMGroupMemberInfo> item = iterator.next();
            stringBuilder.append(getName(e, item.getValue()));
            stringBuilder.append(" ");
          }
          return stringBuilder +
              MyApplication.getInstance().getString(R.string.summary_group_mem_modify);
        }
      case Quit:

        // 退出群聊 不显示
        return NO_TIP_TEXT;
      //return e.getOpUserInfo().getNickName() + " " +
      //    MyApplication.getInstance().getString(R.string.summary_group_mem_quit);
      case ModifyGroupInfo:

        stringBuilder.append(getName(e) + " ");

        List<TIMGroupTipsElemGroupInfo> groupInfoList = e.getGroupInfoList();

        if (null != groupInfoList && groupInfoList.size() > 0) {
          switch (groupInfoList.get(0).getType()) {
            case ModifyName:
              stringBuilder.append("将群名称修改为 " + groupInfoList.get(0).getContent());
              break;
            case ModifyIntroduction:
              stringBuilder.append("修改了群简介");
              break;
            case ModifyFaceUrl:
              stringBuilder.append("修改了群头像");
              break;
            case ModifyNotification:
              stringBuilder.append("修改了公告");
              break;
            case ModifyOwner:
              stringBuilder.append("变更了群主");
              break;
            default:
              stringBuilder.append("修改了群资料");
              break;
          }
        }
        return stringBuilder.toString();
      //return MyApplication.getInstance().getString(R.string.summary_group_info_change);
    }
    return "";
  }

  /**
   * 保存消息或消息文件
   */
  @Override
  public void save() {

  }

  private String getName(TIMGroupTipsElem elem, TIMGroupMemberInfo info) {
    if (info.getNameCard().equals("")) {
      return elem.getOpUserInfo().getNickName();
    }
    return info.getNameCard();
  }

  private String getName(TIMGroupTipsElem elem) {
    TIMGroupMemberInfo info = elem.getOpGroupMemberInfo();

    if (null == info || info.getNameCard().equals("")) {
      return elem.getOpUserInfo().getNickName();
    }
    return info.getNameCard();
  }
}
